// 图层目录树
const LayerTree = new Zondy.Service.GetMapInfoService({
	ip: 'localhost',
	port: 6163,
	mapName: 'GuangGu',
})

//获取地图文档信息
LayerTree.GetMapInfo(function (data) {
	// console.log(data)
	let MapDoc = `
          <a href="javascript:void(0);" onclick="catalogFold(this)">
            <span class="iconfont icon-xiajiantou"></span>
          </a>
            <input type="checkbox" disabled checked>
          <span>地图文档</span>
        <ul>
          <li class="MapDoc">
              <a href="javascript:void(0);" onclick="catalogFold(this)">
              <span class="iconfont icon-xiajiantou"></span>
            </a>
              <input type="checkbox" disabled checked>
              ${data.name}
          </li>
        </ul>
        `
	$('.container_left_content').append(MapDoc)

	// 获取地图信息
	const docCatalog = new Zondy.Catalog.MapDoc({
		ip: 'localhost',
		port: 6163,
		docName: 'GuangGu',
	})
	docCatalog.getMapDocInfo(function (data) {
		// console.log(data)
		let mapItem = `
        <ul>
          <li class="MapItem">
              <a href="javascript:void(0);" onclick="catalogFold(this)">
              <span class="iconfont icon-xiajiantou"></span>
            </a>
              <input type="checkbox" disabled checked>
            ${data.SubMapNames[0]}
            <ul></ul>
          </li>
        </ul>
        `
		$('.MapDoc').append(mapItem)

		// 获取指定地图下所有图层的要素信息
		docCatalog.getLayersInfo(function (data) {
			// 获取要素名字和要素类型
			// console.log(data)
			let layerName = data.value.map((item) => {
				return [item.Name, item.GeomType]
			})

			layerName.forEach(function (value, index, arr) {
				let layerItem = `
          <li>
            <input type="checkbox" checked onclick="layerToggle(this,${index})">
            <img src="./images/${value[1]}.png"> 
            <span>${value[0]}</span>
          </li>`
				$('.MapItem>ul').append(layerItem)
			})
		})
	})
})

// 目录树的显示隐藏
function containerLeftToggle() {
	if ($('.container_left').css('display') == 'none') {
		$('.container_left_control').animate({ left: '200px' }, 300)
		$('.container_left_control').html('<<')
	} else {
		$('.container_left_control').animate({ left: '0' }, 300)
		$('.container_left_control').html('>>')
	}

	$('.container_left').stop().toggle(300)
}

// 目录条目折叠
function catalogFold(obj) {
	// 折叠目录
	$(obj).siblings('ul').stop().slideToggle()
	// 切换名称前的字体图标
	$(obj).children('span').hasClass('icon-xiajiantou')
		? $(obj)
				.children('span')
				.removeClass('icon-xiajiantou')
				.addClass('icon-youjiantou')
		: $(obj)
				.children('span')
				.removeClass('icon-youjiantou')
				.addClass('icon-xiajiantou')
}

// 图层显示隐藏
function layerToggle(e, index) {
	// console.dir(e)
	// console.log(index)
	e.checked
		? docLayer.setLayerStatus(index, 'include')
		: docLayer.setLayerStatus(index, 'exclude')
}
